﻿function CheckInfo() {
    var ret = false;
    ret = volidateConInfo();
    if (ret) {
        if ($("#ContentPlaceHolder2_lkbtnSave").hasClass("aspNetDisabled").toString() != "true") {
            $("#ContentPlaceHolder2_lblMeg").html("数据正在处理,请稍候...");
            if ($(".bankwrap").find("#list_" + $("#ContentPlaceHolder2_hidBranchOfficeUID").val()).size() == 0) { //如果该记录被删除，则拼接 该记录已关联的MediastrUID,及新增UID
                $("#ContentPlaceHolder2_hidAddMediaRelated").val("").val($("#ContentPlaceHolder2_hidRelatedMediastrUID").val() + "," + $("#ContentPlaceHolder2_hidAddMediaRelated").val());
                $("#ContentPlaceHolder2_hidDeleteMediaRelated").val("");
                $("#ContentPlaceHolder2_hidUpdateMediaRelated").val("");
           }
        }
        else {
            ret = false;
        }
    }
    return ret;
}

function ClearText() {
    $("#wrapToggle input[type='text']").val("");
    $("#wrapToggle input:checked").attr("checked", false);
    $(".ImgList").html("");
    var cuid = $("#ContentPlaceHolder1_hdCustomerUID").val();
    $("#wrapToggle input[type='hidden']").val("");
    $("#ContentPlaceHolder1_hdCustomerUID").val(cuid);

    $("#wrapToggle").find("select").each(function (doc) {
        $("#" + $(this).attr("id") + " option:first").attr("selected", true);
        $(this).change();
    });

    $(".form-table").find("input[type='text']").each(function () {
        $(this).focus();
        $(this).blur();
    });
    $(".form-table .activityWrap").css("display", "none");
    $(".form-table").find(".parnt").each(function () {
        $("#" + $(this).attr("id") + " .activityWrap:first").css("display", "block");
    });
}

function QuitToAddBranch() {
    ClearText();
    ToggleDocument('wrapToggle');
}

function AddBranch() {
    ToggleDocument('wrapToggle');
}

function AddImg(type) {
    ToggleDocument('smallImgList2');
    $("#hidAddImgType").val("add");
}


function DeleteBranchOffice(bouid) {

    if (!arguments[0])
        bouid = args1;
    $.ajax({
        url: "/ServerAshxFile/DeleteBranchOffice.ashx",
        data: { UID: bouid },
        beforeSend: function () { },
        success: function (str) {
            var json = { "retValue": "[" + str.toString() + "]" };
            json = eval(json.retValue);
            if (json[0].Issucess == "1") {
                $("#ContentPlaceHolder2_btnRefreshBranchList").click();
            }
            else {
                //输出信息
            }
        }
    });
}

function DeleteRelated(thisval) {
    var mediaRoomRelatedUID = $(thisval).parents("li").attr("id");
    var mediaRoomUID = $(thisval).parents("li").attr("class");
    //begin 记录需要被删除关联的mediaRoomRelatedUID
    var DelIsTure = false;
    var strUIDList = $("#ContentPlaceHolder2_hidRelatedMediastrUID").val().split(',');
    if (strUIDList.length > 0) {
        for (var i = 0; i < strUIDList.length; i++) {
            if (mediaRoomUID == strUIDList[i]) {
                DelIsTure = true;
            }
        }
    }
    if (DelIsTure) {
        $("#ContentPlaceHolder2_hidDeleteMediaRelated").val($("#ContentPlaceHolder2_hidDeleteMediaRelated").val() + mediaRoomRelatedUID + ",");
    }
    // end 记录需要被删除关联的mediaRoomRelatedUID

    //begin 重新记录需要被增加关联的的MediaRoomUID
    var addList = $("#ContentPlaceHolder2_hidAddMediaRelated").val().split(",");
    $("#ContentPlaceHolder2_hidAddMediaRelated").val("");
    if (addList.length > 0) {
        for (var i = 0; i < addList.length; i++) {
            if (mediaRoomUID != addList[i]) {
                $("#ContentPlaceHolder2_hidAddMediaRelated").val($("#ContentPlaceHolder2_hidAddMediaRelated").val() + addList[i] + ",");
            }
        }
    }
    // end 
    $(thisval).parents("li").remove();
    
}

//编辑操作
function EditImgRelated(thisval) {
    ToggleDocument('smallImgList2');
    $("#hidAddImgType").val("edit");
    $("#hidEditRecordUID").val($(thisval).parents("li").attr("id")+ "_" +$(thisval).parents("li").attr("class"));
}

// 关联操作
function ToRelated(thisval) {
    var fileName = $(thisval).find("img").attr("alt");
    var ImgUrl = "/media/upload/branchimg/" + fileName + "_150_150.jpg";
    var imguid = $(thisval).find("img").attr("class");
    var optiontype = $("#hidAddImgType").val();
    var relateduid = $("#hidNeedToEditRelatedUID").val();
    if (optiontype == "add") {//添加按钮进入
        if ($(".ImgList").find("." + imguid).size() == 0) {
            var AddedHtml = "<li class=\"" + imguid + "\"><p class=\"p2\"><img src=\"" + ImgUrl + "\" /></p><div class=\"p4\"><span class=\"showbig\" onclick=\"EditImgRelated(this)\">Edit</span> | <span class=\"closeBtnbig\" onclick=\"DeleteRelated(this)\">Delete</span></div></li>";
            $(".ImgList").html($(".ImgList").html() + AddedHtml);

            //begin 记录需要被增加关联的UID
            var AddIsTure = true;
            var strUIDList = $("#ContentPlaceHolder2_hidRelatedMediastrUID").val().split(',');
            if (strUIDList.length > 0) {
                for (var i = 0; i < strUIDList; i++) {
                    if (imguid == strUIDList[i]) {
                        AddIsTure = false;
                    }
                }
            }
            if (AddIsTure) {
                $("#ContentPlaceHolder2_hidAddMediaRelated").val($("#ContentPlaceHolder2_hidAddMediaRelated").val() + imguid + ",");
            }
            // end 记录需要被增加关联的UID
        }
        else {
            alert("图片：" + fileName + "\n已被添加到列表中.");
        }
    }
    else {   //编辑按钮进入
        if ($(".ImgList").find("." + imguid).size() == 0) {
            var tempEditRecordUID = $("#hidEditRecordUID").val();
            try {
                var str = tempEditRecordUID.split('_');
                var updateHtml = "<p class=\"p2\"><img src=\"" + ImgUrl + "\" /></p><div class=\"p4\"><span class=\"showbig\" onclick=\"EditImgRelated(this)\">Edit</span> | <span class=\"closeBtnbig\" onclick=\"DeleteRelated(this)\">Delete</span></div>";
                $("#" + str[0]).attr("class", imguid);
                $("#" + str[0]).html(updateHtml);
                $("#ContentPlaceHolder2_hidUpdateMediaRelated").val($("#ContentPlaceHolder2_hidUpdateMediaRelated").val() + str[0] + "_" + imguid + ",");
                //记录被修改的关联UID及修改后的MediaRoomUID. 两者之间"_"隔开. 多组之间 ","隔开
            }
            catch (Error) {
                var DelAddUID = tempEditRecordUID.replace("_", ""); //记录需要从添加列表UID需要被修改的UID（删除改UID，并添加修改后的UID到新增UIDList中）
                if (imguid == DelAddUID) {
                    alert("图片：" + fileName + "\n已被添加到列表中.");
                }
                else {
                    var addList = $("#ContentPlaceHolder2_hidAddMediaRelated").val().split(",");
                    $("#ContentPlaceHolder2_hidAddMediaRelated").val("");
                    if (addList.length > 0) {
                        var TrueToAdd = true;
                        for (var i = 0; i < addList.length; i++) {
                            if (DelAddUID == addList[i]) {
                                AddIsTure = false;
                                break;
                            }
                            else {
                                if (imguid == addList[i]) {
                                    AddIsTure = false;
                                    break;
                                }
                                else {
                                    $("#ContentPlaceHolder2_hidAddMediaRelated").val($("#ContentPlaceHolder2_hidAddMediaRelated").val() + addList[i] + ",");
                                }
                            }
                        }
                        if (TrueToAdd) {
                            $("#ContentPlaceHolder2_hidAddMediaRelated").val($("#ContentPlaceHolder2_hidAddMediaRelated").val() + imguid + ",");
                            $("#hidEditRecordUID").val("_" + imguid);
                            var updateHtml = "<p class=\"p2\"><img src=\"" + ImgUrl + "\" /></p><div class=\"p4\"><span class=\"showbig\" onclick=\"EditImgRelated(this)\">Edit</span> | <span class=\"closeBtnbig\" onclick=\"DeleteRelated(this)\">Delete</span></div>";
                            $(".ImgList ." + DelAddUID).html(updateHtml);
                            $(".ImgList ." + DelAddUID).attr("class", imguid);
                        }
                    }
                }
            }
        }
        else {
            alert("图片：" + imguid + "\n已被添加到列表中.");
        }
    } 
}

//获取Video列表
function AddVideo() {
    ToggleDocument('addBoxVide');
    if ($("#ContentPlaceHolder2_hidAllowToOption").attr("value") == "false") {
        $(".ajaxTb").html("<tr><td><span class=\"clsWarningMeg\">暂无数据.</span></td></tr>");
    }
    else {
        var CUID = $("#ContentPlaceHolder2_hidCustomerUID").val();
        $(".ajaxTb").html("<tr><td><span class=\"clsWarningMeg\">正在获取数据,请稍候...</span></td></tr>");
        $.ajax({
            url: "/ServerAshxFile/BranchOffice.ashx",
            type: "GET",
            data: { cuid: CUID, type: "video", optiontype: "get" },
            datatype: "json",
            beforeSend: function () { },
            success: function (str) {
                var json = { "retValue": "[" + str.toString() + "]" };
                var strToTable = "";
                try {
                    json = eval(json.retValue);
                    if (json.length > 0) {
                        for (var i = 0; i < json.length; i++) {
                            strToTable += "<tr><td><input type=\"radio\" name=\"radio\" onclick=\"checkVideo(this)\" value=\"" + json[i].roomUID + "\" id=\"" + json[i].roomUID + "\"><label for=\"" + json[i].roomUID + "\">" + json[i].FileName + "</label></td></tr>";
                        }
                        $(".ajaxTb").html(strToTable);
                    }
                    else {
                        $(".ajaxTb").html("<tr><td><span class=\"clsWarningMeg\">暂无数据</span></td></tr>");
                    }
                }
                catch (Error) {
                    $(".ajaxTb").html("<tr><td height=><span class=\"clsWarningMeg\">" + str + "</span></td></tr>");
                }
            }
        });
    }
}

//Video列表取值，并更改Video文本值
function checkVideo(thi) {
    $("#ContentPlaceHolder2_lblVideoFileName").html(($(thi).next("label").html()));
    $("#ContentPlaceHolder2_hidVideoValue").val($(thi).attr("value"));
}
